/*
 * Copyright (C) 2011 Michael Widmer <widmm8@bfh.ch>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ch.bfh.aeneas.navigation.around;

import android.content.Intent;
import android.location.Location;
import android.util.Log;
import ch.bfh.aeneas.exchange.intent.MalformedIntentException;
import ch.bfh.aeneas.navigation.app.NavigationService;
import ch.bfh.aeneas.exchange.Scope;

/**
 * This is the backing service of AroundMe. It requests the places around 
 * the specified location from providers.
 * @author Michael Widmer <widmm8@bfh.ch>
 */
public class AroundService extends NavigationService {
    
    private String LOG_TAG = "AroundService";
    
    public int whatsAround(Location loc, int range) {
        return sendRequest(loc, range);
    }
    
    private int sendRequest(Location loc, int range, long duration) {
        Intent i = getBroadcastIntent();
        i.putExtra("type", "place");
        i.putExtra("scope",Scope.BOTH);
        i.putExtra("around", loc);
        i.putExtra("range",range);
        
        try {
            broadcast(i, duration);
            return i.getIntExtra("requestId", -1);
        } catch (MalformedIntentException ex) {
            Log.w(LOG_TAG, "Malformed Intent in sendRequest of AroundService!");
            return -1;
        }
    }
    
    private int sendRequest(Location loc, int range) {
        return sendRequest(loc, range, 10000L);
    }
    
    protected void onAnswer(Intent intent) {
        Log.i(LOG_TAG, "Received answer - parsing...");
        informOnResult(intent);
    }
}